package chap03.SelectSort;

/**
 * Created by root on 2017/7/12.
 */
public class ArraySel
{
    private long[] a;                 // ref to array a
    private int nElems;               // number of data items
    //--------------------------------------------------------------
    public ArraySel(int max)          // constructor
    {
        a = new long[max];                 // create the array
        nElems = 0;                        // no items yet
    }
    //--------------------------------------------------------------
    public void insert(long value)    // put element into array
    {
        a[nElems] = value;             // insert it
        nElems++;                      // increment size
    }
    //--------------------------------------------------------------
    public void display()             // displays array contents
    {
        for(int j=0; j<nElems; j++)       // for each element,
            System.out.print(a[j] + " ");  // display it
        System.out.println("");
    }

    public void selectSort()
    {
        int min, out, in;
        int compareNum = 0;
        int exchangeNum = 0;
        for (out=0; out<nElems-1; out++) {
            min=out;
            for (in=out+1; in<nElems; in++) {
                compareNum++;
                if (a[min] > a[in]) {
                    min = in;
                }
            }
            if (min > out)
                exchangeNum++;
                swap(out,min);
        }
        System.out.println("selectSort compareNum is " + compareNum + " 次");
        System.out.println("selectSort exchangeNum is " + exchangeNum + " 次");
        // swap them
    }  // end bubbleSort()

    //--------------------------------------------------------------
    private void swap(int one, int two)
    {
        long temp = a[one];
        a[one] = a[two];
        a[two] = temp;
    }
//--------------------------------------------------------------
}  // end class ArrayBub

